<template>
  <section class="contianer">
    <el-row type="flex" justify="space-between">
      <!-- 顶部过滤列表 -->
      <div class="flights-content">
        <!-- 过滤条件 -->
        <div>
          <FlightsFilter
            v-if="flightsData.options"
            :options="flightsData.options"
            :origin="cacheFlightsList"
            @setFlightsList="setFlightsList"
          />
        </div>

        <!-- 航班头部布局 -->
        <div>
          <FlightsListHead />
        </div>

        <!-- 航班信息 -->
        <div v-if="flightsData.flights && flightsData.flights.length > 0">
          <FlightsItem :data="flight" v-for="(flight,index) in dataList" :key="index" />
        </div>
        <div v-else>暂无航班信息</div>
        <!-- 分页信息 -->
        <el-pagination
          layout="total,sizes,prev, pager, next,jumper"
          :total="flightsData.flights?flightsData.flights.length:0"
          @current-change="currentChange"
          @size-change="sizeChange"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
        ></el-pagination>
      </div>

      <!-- 侧边栏 -->
      <div class="aside">
        <!-- 侧边栏组件 -->
        <FlightsAside />
      </div>
    </el-row>
  </section>
</template>

<script>
import FlightsListHead from "@/components/air/flightsListHead";
import FlightsItem from "@/components/air/flightsItem";
import FlightsFilter from "@/components/air/flightsFilter";
import FlightsAside from "@/components/air/flightsAside";
import moment from "moment";

export default {
  components: {
    FlightsListHead,
    FlightsItem,
    FlightsFilter,
    FlightsAside
  },
  data() {
    return {
      pageIndex: 1,
      pageSize: 10,
      flightsData: {},
      cacheFlightsList: []
    };
  },
  computed: {
    dataList() {
      const beginIndex = (this.pageIndex - 1) * this.pageSize;
      const endIndex = beginIndex + this.pageSize;
      if (this.flightsData.flights) {
        return this.flightsData.flights.slice(beginIndex, endIndex);
      } else {
        return [];
      }
    }
  },
  watch: {
    $route() {
      this.loadPage();
    }
  },
  mounted() {
    this.loadPage();
  },
  methods: {
    loadPage() {
      this.$axios({
        url: "/airs",
        params: this.$route.query
      }).then(res => {
        console.log(res.data);
        this.flightsData = res.data;
        this.cacheFlightsList = [...this.flightsData.flights];
      });
    },
    currentChange(newPageIndex) {
      this.pageIndex = newPageIndex;
    },
    sizeChange(newPageSize) {
      this.pageSize = newPageSize;
    },
    setFlightsList(newFlightsList) {
      this.flightsData.flights = newFlightsList;
    }
  }
};
</script>

<style scoped lang="less">
.contianer {
  width: 1000px;
  margin: 20px auto;
}

.flights-content {
  width: 745px;
  font-size: 14px;
}

.aside {
  width: 240px;
}
</style>